package com.scorekeeper.objects;

import com.scorekeeper.utils.DBProvider;

import android.os.Parcel;
import android.os.Parcelable;

public class Extension implements Parcelable {
	public int _id;
	public String name;
	public String value;
	public int teamId;
	
	private CastType type;
	private int _extType;
	
	public Extension() {
		this._id = -1;
	}
	
	public void setExtType(int newETId) {
		this._extType = newETId;
	}
	
	public int extType() {
		return this._extType;
	}
	
	public enum CastType {
		INT (1), STR (2), LONG (3), BOOL (4);
		
		private final int typeVal;
		
		private CastType(int _val) { 
			typeVal = _val; 
		}
		
		public final int toInt() { 
			return typeVal; 
		}
		
		public static CastType lookup(int typeVal) {
			for (CastType ct: CastType.values()) {
				if (typeVal == ct.toInt()) {
					return CastType.valueOf(ct.name());
				}
			}
			
			return null;
		}
	}
	
	public int describeContents() { return hashCode(); }
	
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeLong(this._id);
		dest.writeString(this.name);
		dest.writeInt(this.type.toInt());
		dest.writeString(this.value.toString());
		dest.writeInt(this.teamId);
		dest.writeInt(this._extType);
	}
	
	public void loadFromParcel(Parcel loadFrom) {
		this._id = loadFrom.readInt();
		this.name = loadFrom.readString();
		this.type = CastType.lookup((int) loadFrom.readInt());
		this.value = loadFrom.readString();
		this.teamId = loadFrom.readInt();
		this._extType = loadFrom.readInt();
	}
	
	public void save() {
		DBProvider db = new DBProvider();
		
		if (this._id != -1) {
			//update
			db.updateObject(DBProvider.Table.EXTENSION, this);
		} else {
			//insert
			db.saveObject(DBProvider.Table.EXTENSION, this);
		}
		
		db.close();
		db = null;
	}
	
	public static class ExtensionConstants {
		public final static String ROW ="ROW";
		public final static String STREAK = "Streak";
		public final static String LASTTEN = "Last10";
		public final static String SO = "S/O";
		public final static String AWAY = "AwayRec";
		public final static String HOME = "HomeRec";
		public final static String GA = "GA";
		public final static String GF = "GF";
		public final static String CLINCH = "Clinched";
	}
}